package controllers.midas;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.Query;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import beans.page.BasePageWrapper;
import beans.page.CommandClassPageWrapper;

import com.google.gson.JsonObject;

import midas.util.DateUtils;
import models.common.user.DASSwBuild;
import controllers.Secure;
import controllers.common.cache.DASCache;

import models.common.user.DASUser;
import models.midas.form.CommandClassForm;
import play.db.jpa.JPA;
import play.mvc.*;
import services.midas.CommandClassService;
import services.midas.MidasStubIntf;
import services.midas.impl.CommandClassServiceImpl;

@With(Secure.class)
public class CommandClass extends Controller {
	
	@Inject
    static MidasStubIntf serviceStub;
	
	@Inject
	static CommandClassService commandClassService;
	
	public static void commandClassData(long id) throws Exception{
		CommandClassForm bean;
		DASUser user = DASUser.findById(1L);//DASCache.getCurrentLogonUser();
    	boolean hasSearch = true;
    	renderArgs.put("states", commandClassService.STATES);
    	renderArgs.put("applications", commandClassService.APPLICATIONS);
    	renderArgs.put("elements", commandClassService.getNetworkElements());
    	renderArgs.put("letters", commandClassService.getAvailableClassLetters());
    	if(id > 0){
    		bean = new CommandClassForm(commandClassService.getCommandClassById(id));
    	}else{
    		bean = commandClassService.initCommandClass();
    	}
    	renderArgs.put("buttons", (new CommandClassPageWrapper(bean,user)).getButtons());
    	render(user,hasSearch,bean);
	}
	
	public static void propose(CommandClassForm bean) throws Exception{
		commandClassService.propose(bean);
		commandClassData(bean.getId());
	}
	
	public static void accept(CommandClassForm bean) throws Exception{
		commandClassService.accept(bean);
		commandClassData(bean.getId());
	}
	
	public static void decline(CommandClassForm bean) throws Exception{
		commandClassService.decline(bean);
		commandClassData(bean.getId());
	}
	
	public static void cancel(CommandClassForm bean) throws Exception{
		commandClassService.cancel(bean);
		commandClassData(bean.getId());
	}
	
	public static void newVersion(CommandClassForm bean) throws Exception{
		commandClassService.newVersion(bean);	
		DASUser user = DASUser.findById(1L);//DASCache.getCurrentLogonUser();
    	boolean hasSearch = true;
    	renderArgs.put("states", commandClassService.STATES);
    	renderArgs.put("applications", commandClassService.APPLICATIONS);
    	renderArgs.put("elements", commandClassService.getNetworkElements());
    	renderArgs.put("letters", commandClassService.getAvailableClassLetters());
    	renderArgs.put("vers", commandClassService.getAvailableVersions());
    	renderArgs.put("buttons", (new CommandClassPageWrapper(bean,user)).getButtons());
    	renderTemplate("/midas/CommandClass/commandClassData.html",user,hasSearch,bean);
	}
	
	public static void checkGrammar(CommandClassForm bean) throws Exception{
		commandClassService.checkGrammar(bean);
		commandClassData(bean.getId());
	}
    
	public static void save(CommandClassForm bean) throws Exception{
		commandClassService.save(bean);
		commandClassData(bean.getId());
    }
	
	public static void gridData(){
		int pageIndex = Integer.parseInt(params.get("grid.page"));
		int pageSize = Integer.parseInt(params.get("grid.pageSize"));
		JSONObject result = commandClassService.getCommandClassList(pageIndex,pageSize);
		renderJSON(result.toString());
	}
	
	public static void swBuildList(String release, String build) {
		List<DASSwBuild> dasSwBuild = commandClassService.getSwBuildList(release, build);
    	renderTemplate("/tags/swBuildList.html", dasSwBuild);
    }
	
	public static void checkIsClassLetterAvailable() throws Exception{
		JSONObject data = new JSONObject();
		data.put("canNew", commandClassService.hasAvailableClassLetter());
		renderJSON(data);
	}
	
	public static void fakeAction(){}
}
